package com.rybbaby.tss.platform.sysHandlePermissions.service.imp;

import com.rybbaby.tss.core.utils.SequenceUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.platform.initializer.HandlePermissionsInitializer;
import com.rybbaby.tss.platform.initializer.SysConfigInitializer;
import com.rybbaby.tss.platform.sysAdminUserJoinSysMenuPermissions.entity.SysAdminUserJoinSysMenuPermissions;
import com.rybbaby.tss.platform.sysAdminUserJoinSysMenuPermissions.service.ISysAdminUserJoinSysMenuPermissionsService;
import com.rybbaby.tss.platform.sysHandlePermissions.entity.SysHandlePermissions;
import com.rybbaby.tss.platform.sysHandlePermissions.service.ISysHandlePermissionsService;
import com.rybbaby.tss.platform.sysHandlePermissionsAttrs.entity.SysHandlePermissionsAttrs;
import com.rybbaby.tss.platform.sysHandlePermissionsAttrs.service.ISysHandlePermissionsAttrsService;
import com.rybbaby.tss.platform.sysHandlePermissionsClasses.entity.SysHandlePermissionsClasses;
import com.rybbaby.tss.platform.sysHandlePermissionsClasses.service.ISysHandlePermissionsClassesService;
import com.rybbaby.tss.platform.sysHandlePermissionsEvents.service.ISysHandlePermissionsEventsService;
import com.rybbaby.tss.platform.sysHandlePermissionsStyles.service.ISysHandlePermissionsStylesService;
import com.rybbaby.tss.platform.sysMenuPermissions.entity.SysMenuPermissions;
import com.rybbaby.tss.platform.sysMenuPermissions.service.ISysMenuPermissionsService;
import com.rybbaby.tss.platform.sysRoleJoinSysMenuPermissions.entity.SysRoleJoinSysMenuPermissions;
import com.rybbaby.tss.platform.sysRoleJoinSysMenuPermissions.service.ISysRoleJoinSysMenuPermissionsService;
import org.apache.commons.lang.BooleanUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 谢进伟
 * @description 操作权限数据库操作服务实现
 * @createDate 2016-11-23 下午11:02:13
 */
@Service
@Transactional
public class SysHandlePermissionsServiceImp extends BaseServiceImp<SysHandlePermissions> implements ISysHandlePermissionsService {

    @Autowired
    private ISysMenuPermissionsService sysMenuPermissionsService;
    @Autowired
    private ISysAdminUserJoinSysMenuPermissionsService sysAdminUserJoinSysMenuPermissionsService;
    @Autowired
    private ISysRoleJoinSysMenuPermissionsService sysRoleJoinSysMenuPermissionsService;
    @Autowired
    private ISysHandlePermissionsAttrsService sysHandlePermissionsAttrsService;
    @Autowired
    private ISysHandlePermissionsClassesService sysHandlePermissionsClassesService;

    @Override
    public void deleteEntityById(String id) {
        // 将分配的权限一并删除
        SysHandlePermissions sysHandlePermissions = this.get(id);
        List<SysHandlePermissions> defaultHandlePermissionId = this.findByProperty("defaultHandlePermissionId", sysHandlePermissions.getId());
        defaultHandlePermissionId.add(sysHandlePermissions);
        this.batchDelete(defaultHandlePermissionId);
    }

    @Override
    public List<Map<String, Object>> adminUserPermissionAllotIdDatasource() {
        String sql1 = "SELECT id,(SELECT smp.name FROM sys_menu_permissions smp WHERE id=saujsmp.menuPermissionId) menuName,(SELECT sau.userName FROM sys_admin_user sau WHERE id=saujsmp.adminuserid) userName FROM sys_admin_user_join_sys_menu_permissions saujsmp;";
        List<Map<String, Object>> adminUserPermissionAllotId_datasources = this.findForJdbc(sql1, null);
        return adminUserPermissionAllotId_datasources;
    }

    @Override
    public List<Map<String, Object>> rolePermissionAllotIdDatasource() {
        String sql2 = "SELECT id,(SELECT smp.name FROM sys_menu_permissions smp WHERE id=srjsmp.menuPermissionsId) menuName,(SELECT sr.roleName FROM sys_role sr where sr.id=srjsmp.roleid) roleName FROM sys_role_join_sys_menu_permissions srjsmp;";
        List<Map<String, Object>> rolePermissionAllotId_datasources = this.findForJdbc(sql2, null);
        return rolePermissionAllotId_datasources;
    }

    @Override
    public List<SysHandlePermissions> saveHandlePermissionAllot(String menuPermissionId, String objType, String objId, String[] handlePermissionIds, String loginUserId) throws Exception {
        List<SysHandlePermissions> newShpList = new ArrayList<>();//新的分配结果
        String adminUserPermissionAllotId = null;
        String rolePermissionAllotId = null;
        List<SysHandlePermissions> alreadyHaveHandlePremissions = new ArrayList<>();//已经分配的结果
        if ("user".equals(objType)) {
            DetachedCriteria dc1 = DetachedCriteria.forClass(SysAdminUserJoinSysMenuPermissions.class);
            dc1.add(Restrictions.and(Restrictions.eq("adminUserId", objId), Restrictions.eq("menuPermissionId", menuPermissionId)));
            List<SysAdminUserJoinSysMenuPermissions> list = this.sysAdminUserJoinSysMenuPermissionsService.findByDetached(dc1);
            if (list != null && !list.isEmpty()) {
                adminUserPermissionAllotId = list.get(0).getId();
                DetachedCriteria dcForAlready = DetachedCriteria.forClass(SysHandlePermissions.class);
                dcForAlready.add(Restrictions.eq("menuId", menuPermissionId));
                dcForAlready.add(Restrictions.eq("adminUserPermissionAllotId", adminUserPermissionAllotId));
                dcForAlready.add(Restrictions.isNotNull("defaultHandlePermissionId"));
                dcForAlready.add(Restrictions.isNull("rolePermissionAllotId"));
                alreadyHaveHandlePremissions = this.findByDetached(dcForAlready);// 管理员已经拥有的操作权限

            }
        } else if ("role".equals(objType)) {
            DetachedCriteria dc2 = DetachedCriteria.forClass(SysRoleJoinSysMenuPermissions.class);
            dc2.add(Restrictions.and(Restrictions.eq("roleId", objId), Restrictions.eq("menuPermissionId", menuPermissionId)));
            List<SysRoleJoinSysMenuPermissions> list = this.sysRoleJoinSysMenuPermissionsService.findByDetached(dc2);
            if (list != null && !list.isEmpty()) {
                rolePermissionAllotId = list.get(0).getId();
                DetachedCriteria dcForAlready = DetachedCriteria.forClass(SysHandlePermissions.class);
                dcForAlready.add(Restrictions.eq("menuId", menuPermissionId));
                dcForAlready.add(Restrictions.eq("rolePermissionAllotId", rolePermissionAllotId));
                dcForAlready.add(Restrictions.isNotNull("defaultHandlePermissionId"));
                dcForAlready.add(Restrictions.isNull("adminUserPermissionAllotId"));
                alreadyHaveHandlePremissions = this.findByDetached(dcForAlready);// 当前角色已经拥有的操作权限
            }
        }
        this.batchDelete(alreadyHaveHandlePremissions);
        if (handlePermissionIds != null && handlePermissionIds.length > 0) {
            // 复制新的操作权限
            DetachedCriteria dc = DetachedCriteria.forClass(SysHandlePermissions.class);
            dc.add(Restrictions.in("id", handlePermissionIds));
            List<SysHandlePermissions> allCheckedShp = this.findByDetached(dc);
            for (SysHandlePermissions shp : allCheckedShp) {
                SysHandlePermissions newShp = new SysHandlePermissions();
                BeanUtils.copyProperties(shp, newShp);
                //关键字段清空
                newShp.setId(null);
                newShp.setCreateUserId(loginUserId);
                newShp.setCreateTime(new Timestamp(System.currentTimeMillis()));
                newShp.setDefaultHandlePermissionId(shp.getId());
                newShp.setAdminUserPermissionAllotId(adminUserPermissionAllotId);
                newShp.setRolePermissionAllotId(rolePermissionAllotId);
                if (StringUtils.isNotBlank(newShp.getAdminUserPermissionAllotId()) || StringUtils.isNotBlank(newShp.getRolePermissionAllotId())) {
                    newShpList.add(newShp);
                }
            }
            this.hibernateBatchSave(newShpList);
        }
        return alreadyHaveHandlePremissions;
    }

    @Override
    public void updateEntitiy(SysHandlePermissions entity) {
        if (entity == null) {
            return;
        }
        if (BooleanUtils.toBoolean(entity.getIsForbidden())) {
            SysMenuPermissions sysMenuPermissions = sysMenuPermissionsService.get(entity.getMenuId());
            if (sysMenuPermissions != null) {
                String handlePermissionIdentifier = sysMenuPermissions.getHandlePermissionIdentifier();
                entity.setHandlePermissionIdentifier(handlePermissionIdentifier);
                HandlePermissionsInitializer.removeCacheHandlePermission(entity);// 移除缓存中的操作权限
            }
        }
    }

    @Override
    public void initMenuDefaultHandle(String menuId, String loginUserId) throws Exception {
        // 搜索操作权限
        initSearchBtn(menuId, loginUserId);
        // 新增作权限
        initAddBtn(menuId, loginUserId);
        // 编辑操作权限
        initEditBtn(menuId, loginUserId);
        // 批量删除
        initBatchDeleteBtn(menuId, loginUserId);
        // 列表删除操作权限
        initListDeleteBtn(menuId, loginUserId);
        // 列表编辑操作权限
        initListEditBtn(menuId, loginUserId);
        // 数据导入
        initImportDataBtn(menuId, loginUserId);
        // 数据导出
        initExportDataBtn(menuId, loginUserId);
    }

    /**
     * 列表删除权限
     *
     * @param menuId      菜单Id
     * @param loginUserId 登录用Id
     * @throws Exception
     */
    private void initListDeleteBtn(String menuId, String loginUserId) throws Exception {
        // 读取默认配置值
        Object defaultListDeleteBtnName = SysConfigInitializer.getConfigValueByConfigName("defaultListDeleteBtnName", "列表行删除");// 列表删除操作权限名称
        Object defaultListDeleteBtnCode = SysConfigInitializer.getConfigValueByConfigName("defaultListDeleteBtnCode", "list_table_tr_delete");// 列表删除操作权限代码
        Object defaultListDeleteBtnInnerHtml = SysConfigInitializer.getConfigValueByConfigName("defaultListDeleteBtnInnerHtml", "<i class=\"fa fa-trash-o\"></i>");// 列表删除操作权限内部html
        Object defaultListDeleteBtnTagName = SysConfigInitializer.getConfigValueByConfigName("defaultListDeleteBtnTagName", "a");// 列表删除操作权限标签名
        Object defaultListDeleteBtnId = SysConfigInitializer.getConfigValueByConfigName("defaultListDeleteBtnId", null);// 列表删除操作权限标Id
        Object defaultListDeleteBtnWeight = SysConfigInitializer.getConfigValueByConfigName("defaultListDeleteBtnWeight", 2);// 列表删除操作权权重
        Object defaultListDeleteBtnIsForbidden = SysConfigInitializer.getConfigValueByConfigName("defaultListDeleteBtnIsForbidden", true);// 列表删除操作是否禁用
        Object defaultListTableTrDeleteBtnClasses = SysConfigInitializer.getConfigValueByConfigName("defaultListTableTrDeleteBtnClasses", "btn btn-default btn-xs icon-only red delete	");// 列表删除操作权限类属性值,多个类名用空格隔开
        // 操作权限
        SysHandlePermissions shp_table_tr_delete = new SysHandlePermissions();
        shp_table_tr_delete.setTagName(defaultListDeleteBtnTagName.toString());
        shp_table_tr_delete.setCode(defaultListDeleteBtnCode.toString());
        shp_table_tr_delete.setName(defaultListDeleteBtnName.toString());
        shp_table_tr_delete.setInnerHtml(defaultListDeleteBtnInnerHtml.toString());
        shp_table_tr_delete.setPageName("");
        shp_table_tr_delete.setId(defaultListDeleteBtnId == null ? "" : defaultListDeleteBtnId.toString());
        shp_table_tr_delete.setWeight(Integer.parseInt(defaultListDeleteBtnWeight.toString()));
        shp_table_tr_delete.setIsForbidden(BooleanUtils.toBooleanObject(defaultListDeleteBtnIsForbidden.toString()));
        shp_table_tr_delete.setCreateTime(new Timestamp(System.currentTimeMillis()));
        shp_table_tr_delete.setCreateUserId(loginUserId);
        shp_table_tr_delete.setMenuId(menuId);
        Serializable handlePermissionId = this.save(shp_table_tr_delete);
        // 操作权限默认类属性
        if (defaultListTableTrDeleteBtnClasses != null && handlePermissionId != null) {
            int weight = 1;
            String[] split = defaultListTableTrDeleteBtnClasses.toString().split("\\s+");
            List<SysHandlePermissionsClasses> list = new ArrayList<SysHandlePermissionsClasses>();
            for (String classs : split) {
                SysHandlePermissionsClasses shpc_tr_delete_btn = new SysHandlePermissionsClasses();
                shpc_tr_delete_btn.setId(SequenceUtil.getLowerCaseUUID());
                shpc_tr_delete_btn.setCreateTime(new Timestamp(System.currentTimeMillis()));
                shpc_tr_delete_btn.setCreateUserId(loginUserId);
                shpc_tr_delete_btn.setHandlePermissionId(handlePermissionId.toString());
                shpc_tr_delete_btn.setIsForbidden(false);
                shpc_tr_delete_btn.setName(classs);
                shpc_tr_delete_btn.setWeight(weight++);
                shpc_tr_delete_btn.setVersion(0);
                list.add(shpc_tr_delete_btn);
            }
            Map<String, String> defaultAttrs = new HashMap<>();
            defaultAttrs.put("targetDataId", ":targetDataId");
            defaultAttrs.put("deleteUrl", ":deleteUrl");
            defaultAttrs.put("serializeNum", ":serializeNum");
            initDefaultAttr(loginUserId, handlePermissionId, defaultAttrs);
            if (!list.isEmpty()) {
                this.sysHandlePermissionsClassesService.jdbcBatchSave(list);
            }
        }
    }

    /**
     * 列表编辑权限
     *
     * @param menuId      菜单Id
     * @param loginUserId 登录用Id
     * @throws Exception
     */
    private void initListEditBtn(String menuId, String loginUserId) throws Exception {
        // 读取默认配置值
        Object defaultListEdditBtnName = SysConfigInitializer.getConfigValueByConfigName("defaultListEdditBtnName", "列表行编辑");// 列表编辑操作权限名称
        Object defaultListEdditBtnCode = SysConfigInitializer.getConfigValueByConfigName("defaultListEdditBtnCode", "list_table_tr_edit");// 列表编辑操作权限代码
        Object defaultListEdditBtnInnerHtml = SysConfigInitializer.getConfigValueByConfigName("defaultListEdditBtnInnerHtml", "<i class=\"fa fa-edit\"></i>");// 索操作权限内部html
        Object defaultListEdditBtnTagName = SysConfigInitializer.getConfigValueByConfigName("defaultListEdditBtnTagName", "a");// 列表编辑操作权限标签名
        Object defaultListEdditBtnId = SysConfigInitializer.getConfigValueByConfigName("defaultListEdditBtnId", null);// 列表编辑操作权限标Id
        Object defaultListEdditBtnWeight = SysConfigInitializer.getConfigValueByConfigName("defaultListEdditBtnWeight", 1);// 列表编辑操作权权重
        Object defaultListEdditBtnIsForbidden = SysConfigInitializer.getConfigValueByConfigName("defaultListEdditBtnIsForbidden", true);// 列表编辑操作是否禁用
        Object defaultListTableTrEditBtnClasses = SysConfigInitializer.getConfigValueByConfigName("defaultListTableTrEditBtnClasses", "btn btn-default btn-xs icon-only green update");// 列表编辑操作权限类属性值,多个类名用空格隔开
        // 操作权限
        SysHandlePermissions shp_table_tr_edit = new SysHandlePermissions();
        shp_table_tr_edit.setTagName(defaultListEdditBtnTagName.toString());
        shp_table_tr_edit.setCode(defaultListEdditBtnCode.toString());
        shp_table_tr_edit.setName(defaultListEdditBtnName.toString());
        shp_table_tr_edit.setInnerHtml(defaultListEdditBtnInnerHtml.toString());
        shp_table_tr_edit.setPageName("");
        shp_table_tr_edit.setId(defaultListEdditBtnId == null ? "" : defaultListEdditBtnId.toString());
        shp_table_tr_edit.setWeight(Integer.parseInt(defaultListEdditBtnWeight.toString()));
        shp_table_tr_edit.setIsForbidden(BooleanUtils.toBooleanObject(defaultListEdditBtnIsForbidden.toString()));
        shp_table_tr_edit.setCreateTime(new Timestamp(System.currentTimeMillis()));
        shp_table_tr_edit.setCreateUserId(loginUserId);
        shp_table_tr_edit.setMenuId(menuId);
        Serializable handlePermissionId = this.save(shp_table_tr_edit);
        // 操作权限默认类属性
        if (defaultListTableTrEditBtnClasses != null && handlePermissionId != null) {
            int weight = 1;
            String[] split = defaultListTableTrEditBtnClasses.toString().split("\\s+");
            List<SysHandlePermissionsClasses> list = new ArrayList<SysHandlePermissionsClasses>();
            for (String classs : split) {
                SysHandlePermissionsClasses shpc_tr_edit_btn = new SysHandlePermissionsClasses();
                shpc_tr_edit_btn.setId(SequenceUtil.getLowerCaseUUID());
                shpc_tr_edit_btn.setCreateTime(new Timestamp(System.currentTimeMillis()));
                shpc_tr_edit_btn.setCreateUserId(loginUserId);
                shpc_tr_edit_btn.setHandlePermissionId(handlePermissionId.toString());
                shpc_tr_edit_btn.setIsForbidden(false);
                shpc_tr_edit_btn.setName(classs);
                shpc_tr_edit_btn.setWeight(weight++);
                shpc_tr_edit_btn.setVersion(0);
                list.add(shpc_tr_edit_btn);
            }
            Map<String, String> defaultAttrs = new HashMap<>();
            defaultAttrs.put("targetDataId", ":targetDataId");
            defaultAttrs.put("editUrl", ":editUrl");
            defaultAttrs.put("serializeNum", ":serializeNum");
            initDefaultAttr(loginUserId, handlePermissionId, defaultAttrs);
            if (!list.isEmpty()) {
                this.sysHandlePermissionsClassesService.jdbcBatchSave(list);
            }
        }
    }

    /**
     * 导出数据按钮权限
     *
     * @param menuId      菜单Id
     * @param loginUserId 登录用Id
     * @throws Exception
     */
    private void initExportDataBtn(String menuId, String loginUserId) throws Exception {
        // 读取默认配置值
        Object defaultExportDataBtnName = SysConfigInitializer.getConfigValueByConfigName("defaultExportDataBtnName", "导出");// 导出操作权限名称
        Object defaultExportDataBtnCode = SysConfigInitializer.getConfigValueByConfigName("defaultExportDataBtnCode", "list_upload");// 导出操作权限代码
        Object defaultExportDataBtnInnerHtml = SysConfigInitializer.getConfigValueByConfigName("defaultExportDataBtnInnerHtml", "<i class=\"fa fa-download\"></i>导出");// 导出操作权限内部html
        Object defaultExportDataBtnTagName = SysConfigInitializer.getConfigValueByConfigName("defaultExportDataBtnTagName", "button");// 导出操作权限标签名
        Object defaultExportDataBtnId = SysConfigInitializer.getConfigValueByConfigName("defaultExportDataBtnId", null);// 导出操作权限标Id
        Object defaultExportDataBtnWeight = SysConfigInitializer.getConfigValueByConfigName("defaultExportDataBtnWeight", 5);// 导出操作权权重
        Object defaultExportDataBtnIsForbidden = SysConfigInitializer.getConfigValueByConfigName("defaultExportDataBtnIsForbidden", true);// 导出操作是否禁用
        Object defaultExportDataBtnClasses = SysConfigInitializer.getConfigValueByConfigName("defaultExportDataBtnClasses", "btn btn-palegreen shiny download");// 导出操作权限类属性值,多个类名用空格隔开
        // 操作权限
        SysHandlePermissions shp_export_data = new SysHandlePermissions();
        shp_export_data.setTagName(defaultExportDataBtnTagName.toString());
        shp_export_data.setCode(defaultExportDataBtnCode.toString());
        shp_export_data.setName(defaultExportDataBtnName.toString());
        shp_export_data.setInnerHtml(defaultExportDataBtnInnerHtml.toString());
        shp_export_data.setPageName("");
        shp_export_data.setId(defaultExportDataBtnId == null ? "" : defaultExportDataBtnId.toString());
        shp_export_data.setWeight(Integer.parseInt(defaultExportDataBtnWeight.toString()));
        shp_export_data.setIsForbidden(BooleanUtils.toBooleanObject(defaultExportDataBtnIsForbidden.toString()));
        shp_export_data.setCreateTime(new Timestamp(System.currentTimeMillis()));
        shp_export_data.setCreateUserId(loginUserId);
        shp_export_data.setMenuId(menuId);
        Serializable handlePermissionId = this.save(shp_export_data);
        // 操作权限默认类属性
        if (defaultExportDataBtnClasses != null && handlePermissionId != null) {
            int weight = 1;
            String[] split = defaultExportDataBtnClasses.toString().split("\\s+");
            List<SysHandlePermissionsClasses> list = new ArrayList<SysHandlePermissionsClasses>();
            for (String classs : split) {
                SysHandlePermissionsClasses shpc_export_btn = new SysHandlePermissionsClasses();
                shpc_export_btn.setId(SequenceUtil.getLowerCaseUUID());
                shpc_export_btn.setCreateTime(new Timestamp(System.currentTimeMillis()));
                shpc_export_btn.setCreateUserId(loginUserId);
                shpc_export_btn.setHandlePermissionId(handlePermissionId.toString());
                shpc_export_btn.setIsForbidden(false);
                shpc_export_btn.setName(classs);
                shpc_export_btn.setWeight(weight++);
                shpc_export_btn.setVersion(0);
                list.add(shpc_export_btn);
            }
            if (!list.isEmpty()) {
                this.sysHandlePermissionsClassesService.jdbcBatchSave(list);
            }
        }
    }

    /**
     * 导入数据按钮权限
     *
     * @param menuId      菜单Id
     * @param loginUserId 登录用Id
     * @throws Exception
     */
    private void initImportDataBtn(String menuId, String loginUserId) throws Exception {
        // 读取默认配置值
        Object defaultImportDataBtnName = SysConfigInitializer.getConfigValueByConfigName("defaultImportDataBtnName", "导入");// 导入操作权限名称
        Object defaultImportDataBtnCode = SysConfigInitializer.getConfigValueByConfigName("defaultImportDataBtnCode", "importData");// 导入操作权限代码
        Object defaultImportDataBtnInnerHtml = SysConfigInitializer.getConfigValueByConfigName("defaultImportDataBtnInnerHtml", "<i class=\"fa fa-upload\"></i>导入");// 导入操作权限内部html
        Object defaultImportDataBtnTagName = SysConfigInitializer.getConfigValueByConfigName("defaultImportDataBtnTagName", "button");// 导入操作权限标签名
        Object defaultImportDataBtnId = SysConfigInitializer.getConfigValueByConfigName("defaultImportDataBtnId", null);// 导入操作权限标Id
        Object defaultImportDataBtnWeight = SysConfigInitializer.getConfigValueByConfigName("defaultImportDataBtnWeight", 6);// 导入操作权权重
        Object defaultImportDataBtnIsForbidden = SysConfigInitializer.getConfigValueByConfigName("defaultImportDataBtnIsForbidden", true);// 导入操作是否禁用
        Object defaultImportDataBtnClasses = SysConfigInitializer.getConfigValueByConfigName("defaultImportDataBtnClasses", "btn btn-palegreen shiny upload");// 导入操作权限类属性值,多个类名用空格隔开
        // 操作权限
        SysHandlePermissions shp_import_data = new SysHandlePermissions();
        shp_import_data.setTagName(defaultImportDataBtnTagName.toString());
        shp_import_data.setCode(defaultImportDataBtnCode.toString());
        shp_import_data.setName(defaultImportDataBtnName.toString());
        shp_import_data.setInnerHtml(defaultImportDataBtnInnerHtml.toString());
        shp_import_data.setPageName("");
        shp_import_data.setId(defaultImportDataBtnId == null ? "" : defaultImportDataBtnId.toString());
        shp_import_data.setWeight(Integer.parseInt(defaultImportDataBtnWeight.toString()));
        shp_import_data.setIsForbidden(BooleanUtils.toBooleanObject(defaultImportDataBtnIsForbidden.toString()));
        shp_import_data.setCreateTime(new Timestamp(System.currentTimeMillis()));
        shp_import_data.setCreateUserId(loginUserId);
        shp_import_data.setMenuId(menuId);
        Serializable handlePermissionId = this.save(shp_import_data);
        // 操作权限默认类属性
        if (defaultImportDataBtnClasses != null && handlePermissionId != null) {
            int weight = 1;
            String[] split = defaultImportDataBtnClasses.toString().split("\\s+");
            List<SysHandlePermissionsClasses> list = new ArrayList<SysHandlePermissionsClasses>();
            for (String classs : split) {
                SysHandlePermissionsClasses shpc_import_btn = new SysHandlePermissionsClasses();
                shpc_import_btn.setId(SequenceUtil.getLowerCaseUUID());
                shpc_import_btn.setCreateTime(new Timestamp(System.currentTimeMillis()));
                shpc_import_btn.setCreateUserId(loginUserId);
                shpc_import_btn.setHandlePermissionId(handlePermissionId.toString());
                shpc_import_btn.setIsForbidden(false);
                shpc_import_btn.setName(classs);
                shpc_import_btn.setWeight(weight++);
                shpc_import_btn.setVersion(0);
                list.add(shpc_import_btn);
            }
            if (!list.isEmpty()) {
                this.sysHandlePermissionsClassesService.jdbcBatchSave(list);
            }
        }
    }

    /**
     * 批量删除按钮操作权限
     *
     * @param menuId      菜单Id
     * @param loginUserId 登录用Id
     * @throws Exception
     */
    private void initBatchDeleteBtn(String menuId, String loginUserId) throws Exception {
        // 读取默认配置值
        Object defaultBatchDeleteBtnName = SysConfigInitializer.getConfigValueByConfigName("defaultBatchDeleteBtnName", "删除");// 删除操作权限名称
        Object defaultBatchDeleteBtnCode = SysConfigInitializer.getConfigValueByConfigName("defaultBatchDeleteBtnCode", "BatchDelete");// 删除操作权限代码
        Object defaultBatchDeleteBtnInnerHtml = SysConfigInitializer.getConfigValueByConfigName("defaultBatchDeleteBtnInnerHtml", "<i class=\"fa fa-trash-o\"></i>删除");// 删除操作权限内部html
        Object defaultBatchDeleteBtnTagName = SysConfigInitializer.getConfigValueByConfigName("defaultBatchDeleteBtnTagName", "button");// 删除操作权限标签名
        Object defaultBatchDeleteBtnId = SysConfigInitializer.getConfigValueByConfigName("defaultBatchDeleteBtnId", null);// 删除操作权限标Id
        Object defaultBatchDeleteBtnWeight = SysConfigInitializer.getConfigValueByConfigName("defaultBatchDeleteBtnWeight", 4);// 删除操作权权重
        Object defaultBatchDeleteBtnIsForbidden = SysConfigInitializer.getConfigValueByConfigName("defaultBatchDeleteBtnIsForbidden", true);// 删除操作是否禁用
        Object defaultDeleteBtnClasses = SysConfigInitializer.getConfigValueByConfigName("defaultDeleteBtnClasses", "btn btn-danger shiny batchDelete");// 删除操作权限类属性值,多个类名用空格隔开
        // 操作权限
        SysHandlePermissions shp_batch_delete = new SysHandlePermissions();
        shp_batch_delete.setTagName(defaultBatchDeleteBtnTagName.toString());
        shp_batch_delete.setCode(defaultBatchDeleteBtnCode.toString());
        shp_batch_delete.setName(defaultBatchDeleteBtnName.toString());
        shp_batch_delete.setInnerHtml(defaultBatchDeleteBtnInnerHtml.toString());
        shp_batch_delete.setPageName("");
        shp_batch_delete.setId(defaultBatchDeleteBtnId == null ? "" : defaultBatchDeleteBtnId.toString());
        shp_batch_delete.setWeight(Integer.parseInt(defaultBatchDeleteBtnWeight.toString()));
        shp_batch_delete.setIsForbidden(BooleanUtils.toBooleanObject(defaultBatchDeleteBtnIsForbidden.toString()));
        shp_batch_delete.setCreateTime(new Timestamp(System.currentTimeMillis()));
        shp_batch_delete.setCreateUserId(loginUserId);
        shp_batch_delete.setMenuId(menuId);
        Serializable handlePermissionId = this.save(shp_batch_delete);
        // 操作权限默认类属性
        if (defaultDeleteBtnClasses != null && handlePermissionId != null) {
            int weight = 1;
            String[] split = defaultDeleteBtnClasses.toString().split("\\s+");
            List<SysHandlePermissionsClasses> list = new ArrayList<SysHandlePermissionsClasses>();
            for (String classs : split) {
                SysHandlePermissionsClasses shpc_delete_btn = new SysHandlePermissionsClasses();
                shpc_delete_btn.setId(SequenceUtil.getLowerCaseUUID());
                shpc_delete_btn.setCreateTime(new Timestamp(System.currentTimeMillis()));
                shpc_delete_btn.setCreateUserId(loginUserId);
                shpc_delete_btn.setHandlePermissionId(handlePermissionId.toString());
                shpc_delete_btn.setIsForbidden(false);
                shpc_delete_btn.setName(classs);
                shpc_delete_btn.setWeight(weight++);
                shpc_delete_btn.setVersion(0);
                list.add(shpc_delete_btn);
            }
            Map<String, String> defaultAttrs = new HashMap<>();
            defaultAttrs.put("batchDeleteUrl", ":batchDeleteUrl");
            defaultAttrs.put("serializeNum", ":serializeNum");
            initDefaultAttr(loginUserId, handlePermissionId, defaultAttrs);
            if (!list.isEmpty()) {
                this.sysHandlePermissionsClassesService.jdbcBatchSave(list);
            }
        }
    }

    /**
     * 编辑权限
     *
     * @param menuId      菜单Id
     * @param loginUserId 登录用Id
     * @throws Exception
     */
    private void initEditBtn(String menuId, String loginUserId) throws Exception {
        // 读取默认配置值
        Object defaultEditBtnName = SysConfigInitializer.getConfigValueByConfigName("defaultEditBtnName", "编辑");// 编辑操作权限名称
        Object defaultEditBtnCode = SysConfigInitializer.getConfigValueByConfigName("defaultEditBtnCode", "edit");// 编辑操作权限代码
        Object defaultEditBtnInnerHtml = SysConfigInitializer.getConfigValueByConfigName("defaultEditBtnInnerHtml", "<i class=\"fa fa-pencil\"></i>编辑");// 编辑操作权限内部html
        Object defaultEditBtnTagName = SysConfigInitializer.getConfigValueByConfigName("defaultEditBtnTagName", "button");// 编辑操作权限标签名
        Object defaultEditBtnId = SysConfigInitializer.getConfigValueByConfigName("defaultEditBtnId", null);// 编辑操作权限标Id
        Object defaultEditBtnWeight = SysConfigInitializer.getConfigValueByConfigName("defaultEditBtnWeight", 3);// 编辑操作权权重
        Object defaultEditBtnIsForbidden = SysConfigInitializer.getConfigValueByConfigName("defaultEditBtnIsForbidden", true);// 编辑操作是否禁用
        Object defaultEditBtnClasses = SysConfigInitializer.getConfigValueByConfigName("defaultEditBtnClasses", "btn btn-magenta shiny update");// 编辑操作权限类属性值,多个类名用空格隔开
        // 操作权限
        SysHandlePermissions shp_edit = new SysHandlePermissions();
        shp_edit.setTagName(defaultEditBtnTagName.toString());
        shp_edit.setCode(defaultEditBtnCode.toString());
        shp_edit.setName(defaultEditBtnName.toString());
        shp_edit.setInnerHtml(defaultEditBtnInnerHtml.toString());
        shp_edit.setPageName("");
        shp_edit.setId(defaultEditBtnId == null ? "" : defaultEditBtnId.toString());
        shp_edit.setWeight(Integer.parseInt(defaultEditBtnWeight.toString()));
        shp_edit.setIsForbidden(BooleanUtils.toBooleanObject(defaultEditBtnIsForbidden.toString()));
        shp_edit.setCreateTime(new Timestamp(System.currentTimeMillis()));
        shp_edit.setCreateUserId(loginUserId);
        shp_edit.setMenuId(menuId);
        Serializable handlePermissionId = this.save(shp_edit);
        // 操作权限默认类属性
        if (defaultEditBtnClasses != null && handlePermissionId != null) {
            int weight = 1;
            String[] split = defaultEditBtnClasses.toString().split("\\s");
            List<SysHandlePermissionsClasses> list = new ArrayList<SysHandlePermissionsClasses>();
            for (String classs : split) {
                SysHandlePermissionsClasses shpc_edit_btn = new SysHandlePermissionsClasses();
                shpc_edit_btn.setId(SequenceUtil.getLowerCaseUUID());
                shpc_edit_btn.setCreateTime(new Timestamp(System.currentTimeMillis()));
                shpc_edit_btn.setCreateUserId(loginUserId);
                shpc_edit_btn.setHandlePermissionId(handlePermissionId.toString());
                shpc_edit_btn.setIsForbidden(false);
                shpc_edit_btn.setName(classs);
                shpc_edit_btn.setWeight(weight++);
                shpc_edit_btn.setVersion(0);
                list.add(shpc_edit_btn);
            }
            Map<String, String> defaultAttrs = new HashMap<>();
            defaultAttrs.put("editUrl", ":editUrl");
            defaultAttrs.put("serializeNum", ":serializeNum");
            initDefaultAttr(loginUserId, handlePermissionId, defaultAttrs);
            if (!list.isEmpty()) {
                this.sysHandlePermissionsClassesService.jdbcBatchSave(list);
            }
        }
    }

    /**
     * 新增按钮权限
     *
     * @param menuId      菜单Id
     * @param loginUserId 登录用Id
     * @throws Exception
     */
    private void initAddBtn(String menuId, String loginUserId) throws Exception {
        // 读取默认配置值
        Object defaultAddBtnName = SysConfigInitializer.getConfigValueByConfigName("defaultAddBtnName", "新增");// 新增操作权限名称
        Object defaultAddBtnCode = SysConfigInitializer.getConfigValueByConfigName("defaultAddBtnCode", "list_add");// 新增作权限代码
        Object defaultAddBtnInnerHtml = SysConfigInitializer.getConfigValueByConfigName("defaultAddBtnInnerHtml", "<i class=\"fa fa-plus\"></i>新增");// 新增作权限内部html
        Object defaultAddBtnTagName = SysConfigInitializer.getConfigValueByConfigName("defaultAddBtnTagName", "button");// 新增操作权限标签名
        Object defaultAddBtnId = SysConfigInitializer.getConfigValueByConfigName("defaultAddBtnId", null);// 新增操作权限标Id
        Object defaultAddBtnWeight = SysConfigInitializer.getConfigValueByConfigName("defaultAddBtnWeight", 2);// 新增操作权权重
        Object defaultAddBtnIsForbidden = SysConfigInitializer.getConfigValueByConfigName("defaultAddBtnIsForbidden", true);// 新增操作是否禁用
        Object defaultAddBtnClasses = SysConfigInitializer.getConfigValueByConfigName("defaultAddBtnClasses", "btn btn-success shiny add");// 新增操作权限类属性值,多个类名用空格隔开
        // 操作权限
        SysHandlePermissions shp_add = new SysHandlePermissions();
        shp_add.setTagName(defaultAddBtnTagName.toString());
        shp_add.setCode(defaultAddBtnCode.toString());
        shp_add.setName(defaultAddBtnName.toString());
        shp_add.setInnerHtml(defaultAddBtnInnerHtml.toString());
        shp_add.setPageName("");
        shp_add.setId(defaultAddBtnId == null ? "" : defaultAddBtnId.toString());
        shp_add.setWeight(Integer.parseInt(defaultAddBtnWeight.toString()));
        shp_add.setIsForbidden(BooleanUtils.toBooleanObject(defaultAddBtnIsForbidden.toString()));
        shp_add.setCreateTime(new Timestamp(System.currentTimeMillis()));
        shp_add.setCreateUserId(loginUserId);
        shp_add.setMenuId(menuId);
        Serializable handlePermissionId = this.save(shp_add);
        // 操作权限默认类属性
        if (defaultAddBtnClasses != null && handlePermissionId != null) {
            int weight = 1;
            String[] split = defaultAddBtnClasses.toString().split("\\s+");
            List<SysHandlePermissionsClasses> list = new ArrayList<SysHandlePermissionsClasses>();
            for (String classs : split) {
                SysHandlePermissionsClasses shpc_add_btn = new SysHandlePermissionsClasses();
                shpc_add_btn.setId(SequenceUtil.getLowerCaseUUID());
                shpc_add_btn.setCreateTime(new Timestamp(System.currentTimeMillis()));
                shpc_add_btn.setCreateUserId(loginUserId);
                shpc_add_btn.setHandlePermissionId(handlePermissionId.toString());
                shpc_add_btn.setIsForbidden(false);
                shpc_add_btn.setName(classs);
                shpc_add_btn.setWeight(weight++);
                shpc_add_btn.setVersion(0);
                list.add(shpc_add_btn);
            }
            Map<String, String> defaultAttrs = new HashMap<>();
            defaultAttrs.put("editUrl", ":editUrl");
            defaultAttrs.put("serializeNum", ":serializeNum");
            initDefaultAttr(loginUserId, handlePermissionId, defaultAttrs);
            if (!list.isEmpty()) {
                this.sysHandlePermissionsClassesService.jdbcBatchSave(list);
            }
        }
    }

    private void initDefaultAttr(String loginUserId, Serializable handlePermissionId, Map<String, String> attrMap) throws Exception {
        List<SysHandlePermissionsAttrs> list_attr = new ArrayList<SysHandlePermissionsAttrs>();
        if (attrMap != null && !attrMap.isEmpty()) {
            Integer index = 0;
            for (String key : attrMap.keySet()) {
                String value = attrMap.get(key);
                SysHandlePermissionsAttrs attrs = new SysHandlePermissionsAttrs();
                attrs.setId(SequenceUtil.getLowerCaseUUID());
                attrs.setCreateUserId(loginUserId);
                attrs.setVersion(index++);
                attrs.setHandlePermissionId(handlePermissionId.toString());
                attrs.setName(key);
                attrs.setValue(":" + value);
                attrs.setIsForbidden(false);
                attrs.setWeight(1);
                attrs.setCreateTime(new Timestamp(System.currentTimeMillis()));
                list_attr.add(attrs);
            }
            this.sysHandlePermissionsAttrsService.jdbcBatchSave(list_attr);
        }
    }

    /**
     * 搜索按钮
     *
     * @param menuId      菜单Id
     * @param loginUserId 登录用Id
     * @throws Exception
     */
    private void initSearchBtn(String menuId, String loginUserId) throws Exception {
        // 读取默认配置值
        Object defaultSearchBtnName = SysConfigInitializer.getConfigValueByConfigName("defaultSearchBtnName", "搜索");// 搜索操作权限名称
        Object defaultSearchBtnCode = SysConfigInitializer.getConfigValueByConfigName("defaultSearchBtnCode", "list_search");// 索操作权限代码
        Object defaultSearchBtnInnerHtml = SysConfigInitializer.getConfigValueByConfigName("defaultSearchBtnInnerHtml", "<i class=\"fa fa-search\"></i>搜索");// 索操作权限内部html
        Object defaultSearchBtnTagName = SysConfigInitializer.getConfigValueByConfigName("defaultSearchBtnTagName", "button");// 搜索操作权限标签名
        Object defaultSearchBtnId = SysConfigInitializer.getConfigValueByConfigName("defaultSearchBtnId", null);// 搜索操作权限标Id
        Object defaultSearchBtnWeight = SysConfigInitializer.getConfigValueByConfigName("defaultSearchBtnWeight", 1);// 搜索操作权权重
        Object defaultSearchBtnIsForbidden = SysConfigInitializer.getConfigValueByConfigName("defaultSearchBtnIsForbidden", true);// 搜索操作是否禁用
        Object defaultSearchBtnClasses = SysConfigInitializer.getConfigValueByConfigName("defaultSearchBtnClasses", "btn btn-info shiny search");// 搜索操作权限类属性值,多个类名用空格隔开
        // 操作权限
        SysHandlePermissions shp_search = new SysHandlePermissions();
        shp_search.setTagName(defaultSearchBtnTagName.toString());
        shp_search.setCode(defaultSearchBtnCode.toString());
        shp_search.setName(defaultSearchBtnName.toString());
        shp_search.setInnerHtml(defaultSearchBtnInnerHtml.toString());
        shp_search.setPageName("");
        shp_search.setId(defaultSearchBtnId == null ? "" : defaultSearchBtnId.toString());
        shp_search.setWeight(Integer.parseInt(defaultSearchBtnWeight.toString()));
        shp_search.setIsForbidden(BooleanUtils.toBooleanObject(defaultSearchBtnIsForbidden.toString()));
        shp_search.setCreateTime(new Timestamp(System.currentTimeMillis()));
        shp_search.setCreateUserId(loginUserId);
        shp_search.setMenuId(menuId);
        Serializable handlePermissionId = this.save(shp_search);
        // 操作权限默认类属性
        if (defaultSearchBtnClasses != null && handlePermissionId != null) {
            int weight = 1;
            String[] split = defaultSearchBtnClasses.toString().split("\\s+");
            List<SysHandlePermissionsClasses> list = new ArrayList<SysHandlePermissionsClasses>();
            for (String classs : split) {
                SysHandlePermissionsClasses shpc_search_btn = new SysHandlePermissionsClasses();
                shpc_search_btn.setId(SequenceUtil.getLowerCaseUUID());
                shpc_search_btn.setCreateTime(new Timestamp(System.currentTimeMillis()));
                shpc_search_btn.setCreateUserId(loginUserId);
                shpc_search_btn.setHandlePermissionId(handlePermissionId.toString());
                shpc_search_btn.setIsForbidden(false);
                shpc_search_btn.setName(classs);
                shpc_search_btn.setWeight(weight++);
                shpc_search_btn.setVersion(0);
                list.add(shpc_search_btn);
            }
            if (!list.isEmpty()) {
                this.sysHandlePermissionsClassesService.jdbcBatchSave(list);
            }
        }
    }

}
